#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int MAXN = 100010;

struct Event {
    int time;
    int id;
    bool isStart;
    
    bool operator<(const Event& other) const {
        if (time != other.time) return time < other.time;
        return isStart > other.isStart; // 结束事件优先处理
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n;
    cin >> n;
    
    vector<pair<int, int>> intervals(n);
    vector<Event> events;
    
    for (int i = 0; i < n; i++) {
        int start, end;
        cin >> start >> end;
        intervals[i] = {start, end};
        events.push_back({start, i, true});
        events.push_back({end, i, false});
    }
    
    sort(events.begin(), events.end());
    
    // 计算总覆盖时间和每个救生员的单独贡献
    int totalCover = 0;
    vector<int> aloneTime(n, 0);
    
    int activeCount = 0;
    int lastTime = 0;
    int singleActive = -1; // 当只有一个救生员活跃时，记录其ID
    
    for (const Event& e : events) {
        if (activeCount > 0) {
            totalCover += e.time - lastTime;
        }
        
        if (activeCount == 1) {
            // 只有一个救生员活跃时，这段时间完全属于这个救生员
            aloneTime[singleActive] += e.time - lastTime;
        }
        
        if (e.isStart) {
            activeCount++;
        } else {
            activeCount--;
        }
        
        // 更新singleActive
        if (activeCount == 1) {
            // 找到当前活跃的救生员
            singleActive = e.id;
        } else {
            singleActive = -1;
        }
        
        lastTime = e.time;
    }
    
    // 找到aloneTime的最小值
    int minAlone = 1e9;
    for (int i = 0; i < n; i++) {
        minAlone = min(minAlone, aloneTime[i]);
    }
    
    // 结果是总覆盖时间减去最小的单独贡献
    int result = totalCover - minAlone;
    cout << result << endl;
    
}